const app = getApp();

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function httpAddUser(url,openId,name,headImg){
  wx.request({
    url: url,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    data: { openId: openId, name: name, headImg: headImg },
    success: function (res) {
      console.log('success')
    }
  });    
}
function httpMine(url, openId, callback){
  wx.request({
    url: url,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    data: { openId: openId },
    success: function (res) {
      callback(res.data);
    }
  });   
}

function http(url,callback){
  wx.request({
    url: url,
    method: 'POST',
    header:{
      "Content-Type": "json"
    },
    success: function(res){
      callback(res.data);
    },
    fail: function(error){
      console.log(error);
    }
  })
}

function httpDetail(url,param,callback){
  wx.request({
    url: url,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    data: { topicId: param },
    success: function (res) {
      callback(res.data);
    }
  });    
}
function httpClassify(url,param,callback){
  wx.request({
    url: url,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    data: { typeId: param },
    success: function (res) {
      callback(res.data);
    }
  });    
}

function httpDianZan(url, param){
  wx.request({
    url: url,
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    },
    method: "POST",
    data: { topicId: param },
    success: function (res) {
      
    }
  });  
}

// function httpComment(url,param){
//   wx.request({
//     url: url,
//     method: "POST",
//     header: {
//       "Content-Type": "application/x-www-form-urlencoded"
//     },
//     method: "POST",
//     data: { openid: , topicId: param },
//     success: function (res) {
      
//     }
//   });  
// }

function avatarImg(imgUrl){
  return app.globalData.antiqueImgUrl + imgUrl;
}

function showImg(arr){
  var imgWrapper = [];
  for (var i in arr){
    var item1 = arr[i];
    var item2 = app.globalData.antiqueImgUrl + item1;
    imgWrapper.push(item2);
  }
  return imgWrapper;
}

function classifyNum(category){
  switch(category) {
    case '云翠珠宝': 
      return 1;
      break;
    case '紫砂陶器':
      return 2;
      break;
    case '书画篆刻':
      return 3;
      break;
    case '古币银元':
      return 4;
      break;
    case '文玩杂项':
      return 5;
      break;
    case '茶酒木艺':
      return 6;
      break;
    case '铜器铁器':
      return 7;
      break;
    case '硬币纸品':
      return 8;
      break;            
  }
}



module.exports = {
  formatTime: formatTime,
  http: http,
  avatarImg: avatarImg,
  httpAddUser: httpAddUser,
  httpMine: httpMine,
  httpDetail: httpDetail,
  httpClassify: httpClassify,
  showImg: showImg,
  classifyNum: classifyNum,
  httpDianZan: httpDianZan
}
